/*
 * @Author: 蔡科 caikeps@163.com
 * @Date: 2024-03-15 22:38:56
 * @LastEditors: 蔡科 caikeps@163.com
 * @LastEditTime: 2024-03-16 14:42:35
 * @FilePath: /go-max-ai-proxy-1/src/db/sqlite.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import { Sql } from "./sql";
// import { readFile } from '../utils/tool'
const sqlite3 = require('better-sqlite3');

export class Sqlite {
    private db;
    constructor() {
        console.log('init sqlite database')
        this.db = new sqlite3('ai-hangzhou.db', { verbose: console.log })

        this.initSql()
    }

    private initSql() {
        console.log(Sql)

        const apps = this.db.exec(Sql.apps)//执行sql命令
        const result = this.db.exec(Sql.category)//执行sql命令
        console.log(apps)
        console.log(result)

        // this.initData()
    }


    // private async initData() {

    //     const sql = 'INSERT INTO apps (id, name, way, category_id, app_type, star, status, is_collect, groups, created_at, logo, desc) VALUES (@id, @name, @way, @category_id, @app_type, @star, @status, @is_collect, @groups, @created_at, @logo, @desc)'

    //     const data: { appData: any[] } = await readFile('data/json/app.json')
    //     console.log(data)

    //     const result = await this.insertBatch(sql, data.appData.map(item => {
    //         return {
    //             ...item,
    //             group: '',
    //             groups: item.group.join(","),
    //         }
    //     }))
    //     console.log(result)

    // }

    public async findOne(sql: string) {

        console.log(sql)

        return new Promise((resolve) => {
            const stmt = this.db.prepare(sql);
            const one = stmt.get();
            resolve(one)
        })
    }

    public async insert(sql: string, data: any) {

        console.log(sql)

        return new Promise((resolve) => {
            const insert = this.db.prepare(sql);

            insert.run(data)
        })
    }

    public async insertBatch(sql: string, data: any[]) {

        console.log(sql)

        return new Promise((resolve) => {
            // 'INSERT INTO cats (name, age) VALUES (@name, @age)'
            const insert = this.db.prepare(sql);
            const insertMany = this.db.transaction((cats: any) => {
                console.log(cats)
                for (const cat of cats) {
                    insert.run(cat);
                }
            });

            const result = insertMany(data)
            resolve(result)
        })
    }

    public async delete(sql: string, data: { id: number }) {

        console.log(sql)
        // 'DELETE FROM cats WHERE id = @id;
        return new Promise((resolve) => {
            const deleteStmt = this.db.prepare(sql)
            const result = deleteStmt.run(data)

            console.log(result)
            resolve(result)
        })
    }


    public async update(sql: string, data: any) {

        console.log(sql)
        // UPDATE cats SET name =@name ,age=@age WHERE id=@id'

        return new Promise((resolve) => {

            const updateStmt = this.db.prepare(sql)
            const result = updateStmt.run(data)
            console.log(result)
            resolve(result)
        })
    }
}